window.pageRoot;
window.isBtnTweening = false;
var outPage = "";
window.page = new Object();
var pageWidth = 640;
var pageHeight = 1040;
window.isClicking = false;
function isMovieClip(obj) {
    if (obj.instanceType == "annie.MovieClip") {
        return true;
    } else {
        return false;
    }
};

function showPop(pageName, direction, callback) {
    // console.info("showPop");
    window.pageRoot.addChildAt(page[pageName], pageRoot.children.length - 1);
    page[pageName].visible = true;
    page[pageName].alpha = 1;
    page[pageName].direction = direction;
    page[pageName].x = page[pageName].initX;
    page[pageName].y = page[pageName].initY;
    page[pageName].scaleX = 1;
    page[pageName].scaleY = 1;
    var tweenTime = 300;
    //console.log('page[pageName].alpha=',page[pageName].alpha,'     page[pageName].initY=',page[pageName].initY);
    if (direction == "R") {
        page[pageName].x = 720;
        annie.Tween.to(page[pageName], tweenTime / 1000, {
            x: page[pageName].initX,
            ease: annie.Tween.exponentialInOut
        });
    } else if (direction == "L") {
        page[pageName].x = -880;
        annie.Tween.to(page[pageName], tweenTime / 1000, {
            x: page[pageName].initX,
            ease: annie.Tween.exponentialInOut
        });
    } else if (direction == "T") {
        page[pageName].y = -1430;
        annie.Tween.to(page[pageName], tweenTime / 1000, {
            y: page[pageName].initY,
            ease: annie.Tween.exponentialInOut
        });
    } else if (direction == "B") {
        page[pageName].y = 1170;
        annie.Tween.to(page[pageName], tweenTime / 1000, {y: -130, ease: annie.Tween.exponentialInOut});
    } else if (direction == "A") {
        page[pageName].alpha = 0;
        annie.Tween.to(page[pageName], 0.2, {alpha: 1});
    } else if (direction == "S") {
        page[pageName].alpha = 0.5;
        page[pageName].x = page[pageName].x + (800 * 0.4) / 2;
        page[pageName].y = page[pageName].y + (1300 * 0.4) / 2;
        page[pageName].scaleX = .6;
        page[pageName].scaleY = .6;
        annie.Tween.to(page[pageName], tweenTime / 2000, {
            x: -80,
            y: -130,
            scaleX: 1,
            scaleY: 1,
            alpha: 1,
            ease: annie.Tween.sinusoidalInOut
        });
    } else if (direction == "P") {
        // if (isMovieClip(page[pageName])) {
        //     page[pageName].gotoAndPlay(2);
        // }
    }
    if (isMovieClip(page[pageName])) {
        page[pageName].gotoAndPlay(2);
    }
    if (callback) {
        setTimeout(callback, (tweenTime * 2 + 10));
    }
};

function hidePop(pageName, direction, callback) {
    page[pageName].alpha = 1;
    page[pageName].scaleX = 1;
    page[pageName].scaleY = 1;
    page[pageName].initX = page[pageName].x;
    page[pageName].initY = page[pageName].y;
    var tweenTime = 300;
    if (direction == "R") {
        annie.Tween.to(page[pageName], tweenTime / 1000, {x: 720, ease: annie.Tween.exponentialInOut});
    } else if (direction == "L") {
        annie.Tween.to(page[pageName], tweenTime / 1000, {x: -880, ease: annie.Tween.exponentialInOut});
    } else if (direction == "T") {
        annie.Tween.to(page[pageName], tweenTime / 1000, {y: -1430, ease: annie.Tween.exponentialInOut});
    } else if (direction == "B") {
        annie.Tween.to(page[pageName], tweenTime / 1000, {y: 1170, ease: annie.Tween.exponentialInOut});
    } else if (direction == "A") {
        annie.Tween.to(page[pageName], 0.2, {alpha: 0});
    } else if (direction == "S") {
        annie.Tween.to(page[pageName], tweenTime / 2500, {
            x: page[pageName].x + (800 * 0.4) / 2,
            y: page[pageName].y + (1300 * 0.4) / 2,
            scaleX: .6,
            scaleY: .6,
            alpha: 0,
            ease: annie.Tween.sinusoidalInOut
        });
    } else {
        tweenTime = 1;
    }
    setTimeout(hidePopComplete, tweenTime * 2,pageName);
    if (callback) {
        setTimeout(callback, tweenTime * 2);
        callback = null;
    }
};

function hidePopComplete(pageName) {
    page[pageName].visible = false;
    if (isMovieClip(page[pageName])) {
        page[pageName].gotoAndStop(1);
    }
    page[pageName].x = 0;
    page[pageName].y = 0;
};

function showPage(pageName, direction, callback, isPlayMov) {
    var tweenTime = 400;
    //console.info(onPage,pageName,tweenTime,pageHeight,page[pageName].initY);
    if (onPage != pageName) {

        page[pageName].x = page[pageName].initX;
        page[pageName].y = page[pageName].initY;
        page[pageName].alpha = 1;
        if (direction != "A") {
            if (window.pageRoot.getChildIndex(page[onPage]) > window.pageRoot.getChildIndex(page[pageName])) {
                // this.setChildIndex(page[pageName], this.getChildIndex(page[onPage]))
                window.pageRoot.addChildAt(page[pageName], window.pageRoot.getChildIndex(page[onPage]));
            }
        }

        if (direction == "R") {
            page[pageName].x = pageWidth;
            annie.Tween.to(page[onPage], tweenTime / 1000, {
                x: -880,
                ease: annie.Tween.exponentialInOut,
                delay: 0.1
            });
            annie.Tween.to(page[pageName], tweenTime / 1000, {
                x: page[pageName].initX,
                ease: annie.Tween.exponentialInOut
            });
        } else if (direction == "L") {
            page[pageName].x = -pageWidth;
            annie.Tween.to(page[onPage], tweenTime / 1000, {
                x: 720,
                ease: annie.Tween.exponentialInOut,
                delay: 0.1
            });
            annie.Tween.to(page[pageName], tweenTime / 1000, {
                x: page[pageName].initX,
                ease: annie.Tween.exponentialInOut
            });
        } else if (direction == "T") {
            page[pageName].y = -1430;
            annie.Tween.to(page[onPage], tweenTime / 1000, {
                y: 1170,
                ease: annie.Tween.exponentialInOut,
                delay: 0.1
            });
            annie.Tween.to(page[pageName], tweenTime / 1000, {
                y: page[pageName].initY,
                ease: annie.Tween.exponentialInOut
            });
        } else if (direction == "B") {
            page[pageName].y = 1170;
            annie.Tween.to(page[onPage], tweenTime / 1000, {
                y: -1430,
                ease: annie.Tween.exponentialInOut,
                delay: 0.1
            });
            annie.Tween.to(page[pageName], tweenTime / 1000, {
                y: page[pageName].initY,
                ease: annie.Tween.exponentialInOut
            });
        } else if (direction == "A") {
            page[pageName].visible = true;
            annie.Tween.to(page[onPage], 1, {
                alpha: 0
            });
        }

        isPlayMov = true;
        if (direction != "A") {
            setTimeout(function (e) {
                page[pageName].visible = true;
            }, 50 * 2);
            outPage = onPage;
            onPage = pageName;
            setTimeout(hidePage, tweenTime * 2,outPage);
            if (isMovieClip(page[pageName]) && isPlayMov == true) {
                setTimeout(moviePagePlay(page[pageName]), tweenTime * 1.5);
            }
        } else {
            outPage = onPage;
            onPage = pageName;
            setTimeout(hidePage, 1100, outPage);
            if (isMovieClip(page[pageName]) && isPlayMov == true) {
                moviePagePlay(page[pageName]);
            }
        }
        if (callback) {
            setTimeout(callback, tweenTime * 2);
        }
    }
};

function moviePagePlay(moviePage) {
    if (isMovieClip(moviePage)) {
        moviePage.gotoAndPlay(2);
    }
};

function hidePage(pageName) {
    page[pageName].visible = false;
    page[pageName].y = -130;
    page[pageName].x = -80;
    if (isMovieClip(page[pageName])) {
        page[pageName].gotoAndStop(1);
    }
};

function btnDown(e) {
    // console.log("btnName:" + e.currentTarget.name);
    // console.log("=====btnName:", e.currentTarget);
    if (window.isClicking) {
        return;
    }
    window.isClicking = true;

    setTimeout(function (ev) {
        window.isClicking = false;
    }, 400);

    if (!window.isBtnTweening) {
        window.isBtnTweening = true;

        var tweenTime = 80;
        var obj = e.currentTarget;
        if (obj.name.indexOf("feiBtn") >= 0) {//如果有一些按钮不想要点击动效，就直接设置命名
            pageRoot.btnDownFun(obj);
        } else {

            obj.initX = obj.x;
            obj.initY = obj.y;
            annie.Tween.to(obj, 0.04, {
                //alpha     : 0.5,
                x: obj.x + (obj.width * 0.1) / 2,
                y: obj.y + (obj.height * 0.1) / 2,
                scaleX: 0.9,
                scaleY: 0.9,
                onComplete: function (e) {
                    annie.Tween.to(obj, 0.04, { x: obj.initX, y: obj.initY, scaleX: 1, scaleY: 1});
                }
            });
            setTimeout(pageRoot.btnDownFun, tweenTime * 2, obj);
        }
    }
};

var startPoint = null;
document.addEventListener("touchstart", function (e) {
    var e = e || window.event;
    startPoint = e.touches[0];
});
document.addEventListener("touchend", function (e) {
    var e = e || window.event;
    //e.changedTouches能找到离开手机的手指，返回的是一个数组
    var endPoint = e.changedTouches[0];
    //计算终点与起点的差值
    var x = endPoint.clientX - startPoint.clientX;
    var y = endPoint.clientY - startPoint.clientY;
    //设置滑动距离的参考值
    var d = 100;
    if (Math.abs(x) > d) {
        if (x > 0 && (y < 10 || y > 10)) {
            swiperRight();
        } else if (x < 0 && (y < 10 || y > 10)) {
            swiperLeft();
        }
    }
    if (Math.abs(y) > d) {
        if (y > 0) {
            swiperBottom();
        } else {
            swiperUp();
        }
    }
});


function swiperLeft() {};

function swiperRight() {};

function swiperUp() {};
function swiperBottom() {};

var u = navigator.userAgent;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
window.config.isAndroid = isAndroid;


//压缩图片
//参数  Img dom  type image/jpg  image/png
//quality 质量 0-1
//mx mh 最大宽  高
function compressImg(img, type, quality,mx, mh) {
    const canvas = document.createElement('canvas');
    const context = canvas.getContext('2d');
    const { naturalWidth: originWidth, naturalHeight: originHeight } = img;
    // 最大尺寸限制
    const maxWidth = mx;
    const maxHeight = mh;
    // 目标尺寸
    let targetWidth = originWidth;
    let targetHeight = originHeight;
    if (originWidth > maxWidth || originHeight > maxHeight) {
        if (originWidth / originHeight > 1) {
            // 宽图片
            targetWidth = maxWidth;
            targetHeight = Math.round(maxWidth * (originHeight / originWidth))
        } else {
            // 高图片
            targetHeight = maxHeight;
            targetWidth = Math.round(maxHeight * (originWidth / originHeight))
        }
    }
    canvas.width = targetWidth;
    canvas.height = targetHeight;
    context.clearRect(0, 0, targetWidth, targetHeight);
    // 图片绘制
    context.drawImage(img, 0, 0, targetWidth, targetHeight);
    // canvas.toBlob(function(res){
    //     if(res && res.size){
    //         console.log("压缩后:"+res.size);
    //     }
    // },type,quality);
    return canvas.toDataURL(type,quality);
}
